package com.jcx.ldzj.utile;

import com.jcx.ldzj.emun.SystemCode;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

/**
 * AES加密工具类
 * Author: ld
 * Date: 2020-07-21 14:37
 */
public class AESUtile
{
    public static SimpleHash MD5Utile(String password){
        SimpleHash simpleHash=new SimpleHash("MD5", ByteSource.Util.bytes(password), SystemCode.ADD_SALT,1);
        return simpleHash;
    }

    public static String Encrypt(String sSrc, String sKey, String offset)
    {
        try
        {
            if (checkParams(sKey)) return null;
            byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
            IvParameterSpec iv = new IvParameterSpec(offset.getBytes(StandardCharsets.UTF_8));// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));

            return Base64.encodeBase64String(encrypted);// 此处使用BASE64做转码功能，同时能起到2次加密的作用。
        } catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public static String Decrypt(String sSrc, String sKey, String offset)
    {
        try
        {
            if (checkParams(sKey)) return null;
            byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(offset.getBytes(StandardCharsets.UTF_8));
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = Base64.decodeBase64(sSrc);// 先用BAES64解密
            try
            {
                byte[] original = cipher.doFinal(encrypted1);
                return new String(original, StandardCharsets.UTF_8);
            } catch (Exception e)
            {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }

    private static boolean checkParams(String sKey)
    {
        // 判断Key是否正确
        if (sKey == null)
        {
            return true;
        }
        // 判断Key是否为16位
        return sKey.length() != 16;
    }

//    public static void main(String[] args) {
//        SimpleHash simpleHash=new SimpleHash("MD5", ByteSource.Util.bytes("密码"),"salt加盐",3);
//        System.out.println(simpleHash);
//    }
//    public static void main(String[] args) throws JsonProcessingException
//    {
//        Map<String, Object> param = new HashMap<>();
//        param.put("vin", "LFWSRXSH7HAD36249");
//        param.put("receivedTime",1536289568000L);
//        String jsonStr = JSONObject.toJSONString(param);
//        System.out.println("原始内容：" + jsonStr);
//        long l = System.currentTimeMillis() / 1000;
//        System.out.println(l);
//
//        String a = "{\n" +
//                "    \"VIN\": \"dasdasd121313dasda\",\n" +
//                "    \"commandNo\": \"0xC2\",\n" +
//                "    \"content\":\"1A2B3C4D\",\n" +
//                "    \"requestTime\":\"1595320983\"\n" +
//                "}";
//        Map<String, String> b = new HashMap();
//        b.put("VIN", "dasdasd121313dasda");
//        b.put("commandNo", "0xC2");
//        b.put("content", "1A2B3C4D");
//        b.put("requestTime", "1595320983");
//
//        ObjectMapper mapper = new ObjectMapper();
//        String s = mapper.writeValueAsString(b);
//
//        Map map = mapper.readValue(a, Map.class);
//        System.out.println(map);
//
////        AESUtil.Encrypt(, )
//        System.out.println(a);
//        System.out.println("-----------------------------------------" + System.lineSeparator());
//
//        System.out.println(s);
////        System.out.println("requestTime: " + l);
//        String temp = AESUtil.Encrypt(s, "CNXP1XNjn3023MXB", "BNcS28Ta933JFD2W");
//        System.out.println(temp);
//        String decrypt = AESUtil.Decrypt(temp, "CNXP1XNjn3023MXB", "BNcS28Ta933JFD2W");
        //System.out.println(decrypt);
//        System.out.println("加密后内容：" + temp);
//        System.out.println("解密后内容：" + AESUtil.Decrypt("So0LmH/fpDfzijGrQUHHCYXzAwDV0SKtNVDwCzT8weiic2BExltFNK1BPnv4cIucsOSG3OYHpYYZ9oPWA+6aQw==", "1234567890abcdef", "1234567890abcdef"));
//    }
}

